<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no">
    <title>物资报废</title>
    <link rel="stylesheet" href="css/public.css">
    <style>
        .delete {
            position: relative;
            overflow: hidden;
        }

        .delete h2 {
            position: relative;
            top: 0.44rem;
            left: 0.7rem;
            font-size: 0.34rem;
            color: black;
            margin: 0;
        }

        .delete input {
            border-radius: 0.15rem;
            width: 6.0rem;
            border: none;
            height: 0.56rem;
            box-shadow: inset 0.5px 0.5px 0.5px #c7c6;
            background: #F7F7F7;
        }

        button {
            width: 5.46rem;
            height: 0.86rem;
            margin: 0 auto;
            position: relative;
            background: #3D88DD;
            display: block;
            color: white;
            font-size: 0.3rem;
            border-radius: 0.2rem;
            box-shadow: 0px 2px 4px 1px #4994e7bd;
            border: none;
            margin-top: 0.58rem;
        }

        a {
            color: white;
            display: inline-block;
            width: 100%;
            text-decoration: none;
        }

        .my_ml_list {
            width: 93%;
            font-size: 12px;
            margin: 0 auto;
            height: 100%;
            vertical-align: middle;
        }

        .my_ml_list span {
            font-size: 0.2rem;
            vertical-align: middle;
        }

        .model {
            margin-left: 0.36rem;
            width: 1.6rem;
            display: inline-block;
        }

        .model p {
            margin: 0;
            word-break: break-all;
        }

        .add_reduce {
            float: right;
            background: #dedede;
            height: 0.5rem;
            display: inline-block;
            margin-top: 0.2rem;
        }

        .add_reduce span {
        }

        .add_reduce input {
            width: 0.45rem;
            border-radius: 0;
            box-shadow: none;
            background: #f4f4f4;
            outline: none;
            padding: 0;
            height: 100%;
            border: 0px;
        }

        .card_tittle {
            position: relative;
            font-size: 16px;
        }

        .card_tittle span {
            font-size: 0.25rem;
            display: inline-block;
            margin-top: 0.16rem;
        }

        .card_content {
            width: 6.8rem;
            height: 5.62rem;
            overflow: scroll;
            margin: 0.66rem auto;
            border-radius: 0.2rem;
            box-shadow: 1px 1px 10px 2px #eceaea;
        }

        .card_content ul {
            list-style: none;
            margin: 0;
            padding: 0;
            font-size: 0.32rem;
        }

        .card_content hr {
            width: 90%;
            margin: 0.16rem auto;
            border: none;
            background-color: #e4e4e4;
            height: 1px;
        }

        .dived {
            height: 0.1rem;
            background: #eae7e7;
        }
    </style>
</head>

<body>
<div class="delete">
    <h2>我的物资</h2>
    <div class="card_content">
        <div class="card_tittle">
            <span style="margin-left:0.24rem;margin-right: 0.4rem">名称</span>
            <span style="margin-right: 0.7rem">规格型号</span>
            <span style="margin-right: 0.4rem">采购时间</span>
            <span style="margin-right: 0.4rem">数量</span>
            <span>选择数量</span>
        </div>
        <hr>
        <ul class="delete-list">

        </ul>
    </div>
    <div class="dived"></div>
        <h2 style="top:0.2rem">报废原因</h2>
        <input class="delete_cause"  type="text" style="position: relative;display: block;top:0.4rem;left: 0.7rem;height: 0.64rem">
        <h2  style="margin-right: 0.32rem;margin-top:0.32rem">残值评估
            <input  class="delete_price" style="width:2.6rem;margin-left: 0.32rem;margin-right: 0.12rem" type="text">元</h2>
        <h2 style="margin-top: 0.2rem">添加照片</h2>
        <input class="camera_input" onchange="uploadImg();" type="file" accept="image/*" id="photo" name="">
        <div class="camera_div">
            <span id="photo_name"></span>
            <img class="camera" src="images/camera.png">
        </div>
</div>
<button id="go_delete">提交申请</button>
<script src="http://libs.baidu.com/jquery/1.11.3/jquery.min.js"></script>
<script type="application/javascript">
    document.documentElement.style.fontSize = document.documentElement.clientWidth / 7.5 + 'px';

    function getQueryString(name) {

        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return unescape(r[2]); return null;
    }
    var oAccessToken = getQueryString('access-token');

   // 获取列表
    $.ajax({
        url:"/v1/goods/scrap-list?access-token="+oAccessToken,
        type:'post',
        data:'',
        async:true,
        success:function(data){
            var len = data.body.length;
            if(data.status == 200){
                var html='';
                for(let i=0;i<len;i++){

                html += '<li class="my_ml_list" id='+data.body[i].id+'><span>'+data.body[i].name+'</span><span class="model"> <p>'+data.body[i].scale+'</p><p>'+data.body[i].type+'</p></span>'+
                        '<span style="margin-left: 0.28rem">'+data.body[i].buy_time+'</span><span class="my_product_num" style="margin-left: 0.68rem">'+data.body[i].num+'</span>'+
                        '<span class="add_reduce"> <span onclick="numDec(this)"><img style="width:0.2rem;margin-left: 0.1rem"  src="./images/reduce.png"></span>'+
                        '<input style="text-align: center;" type="text" class="quantity" value="0" /><span onclick="numAdd(this)"><img style="width:0.2rem;margin-right: 0.1rem;"  src="./images/add.png"></span></span>'+
                        '<hr style="width: 100%;"></li>';
                }
                $('.delete-list').html(html);
            }
        },
        error:function(data){
            if(data.status == 401){
                window.location.href = './index.html';
            }
        },
    });

        function submit_info(imgUrl){
            var aList = $('.my_ml_list');
            var aNum  = $('.quantity');
            for(var i=0;i<aList.length;i++){
                if( $(aNum[i]).val() != 0 ){
                    var Id  = $(aList[i]).attr('id');
                    var Num = $(aNum[i]).val();
                }
            }
            $.ajax({
                url:'/v1/goods/scrap-apply?access-token='+oAccessToken,
                type:'post',
                data:{
                    id:Id,
                    num:Num,
                    reason:$('.delete_cause').val(),
                    salvage:$('.delete_price').val(),
                    image:imgUrl,
                },
                success:function(data){
                    if(data.status == 200){
                        window.location.href = './Result_delete.html?access-token='+oAccessToken;
                    }
                }
            })
        }

    $('#go_delete').click(function () {
        upload_img();
    })
    function upload_img(){
        var myImg = $('.camera_input').get(0).files[0];
        var reader = new FileReader();
        reader.readAsDataURL(myImg);
        reader.onload = function (e) {
            new_img_src = e.target.result;
            dataImg = new_img_src.substring(22);
            $.ajax({
                type: 'post',
                url: './filesave.php',
                data: {dataimg: dataImg, datatype: 'img'},
                beforeSend: function () {
                },
                success: function (str) {
                    // 图片上传成功后上传其他信息，并将图片路径发送给后台
                    submit_info(str)
                }
            });
        };
    }
    function uploadImg() {
        var obj   = document.getElementById("photo");
        var len   = obj.files.length;
        for (var i = 0; i < len; i++) {
            var temp   = obj.files[i].name;
            document.getElementById("photo_name").innerHTML = "<span>" + temp + "</span>"
        }
    }

    /*物资数量+1*/
    function numAdd(e) {
        var quantity = $(e).siblings('.quantity').val();
        var num_add = parseInt(quantity) + 1;
        if (quantity == "") {
            num_add = 1;
        }else if (quantity == $(e).parent().siblings(".my_product_num").text()) {
            var num_add = quantity;

        }
        $(e).siblings('.quantity').val(num_add);
        $(e).parents(".my_ml_list").siblings("li").find('.quantity').val(0);
    }

    /*物资数量-1*/
    function numDec(e) {
        var quantity = $(e).siblings('.quantity').val();
        var num_dec = parseInt(quantity) - 1;
        if (quantity == "" || quantity <= 0) {
            num_dec = 0;
        }
        $(e).siblings('.quantity').val(num_dec);

    }

</script>
</body>

</html>